package off_transaction

import (
	// "bp/cache"
	sqlc "bp/db/sqlc"
	// "bp/internal/util"
	"context"
)

// DisableOffTransactionDetailsByOutTransNo 根据外部交易号作废优惠交易明细
func (s *OffTransactionEntityServiceImp) DisableOffTransactionDetailsByOutTransNo(ctx context.Context, q *sqlc.Queries, outTransNo string) (err error) {
	// slog := util.NewContextLogger(ctx, s.slog)
	err = q.DisableOffTransactionDetailsByOutTransNo(ctx, outTransNo)
	if err != nil {
		return
	}
	return
}

type DisableOffTransactionDetailsByOutTransNoAndOutOrderNoParam struct {
	OutTransNo string
	OutOrderNo string
}

// DisableOffTransactionDetailsByOutTransNoAndOutOrderNo 根据外部交易号+外部订单号作废优惠交易明细
func (s *OffTransactionEntityServiceImp) DisableOffTransactionDetailsByOutTransNoAndOutOrderNo(ctx context.Context, q *sqlc.Queries, ptr *DisableOffTransactionDetailsByOutTransNoAndOutOrderNoParam) (err error) {
	// slog := util.NewContextLogger(ctx, s.slog)
	err = q.DisableOffTransactionDetailsByOutTransNoAndOutOrderNo(ctx, sqlc.DisableOffTransactionDetailsByOutTransNoAndOutOrderNoParams{
		OutTransNo: ptr.OutTransNo,
		OutOrderNo: ptr.OutOrderNo,
	})
	if err != nil {
		return
	}
	return
}

type RefreshOffTransactionDetailsByOutTransNoAndOutOrderNoParam struct {
	OutTransNo                           string
	OutOrderNo                           string
	UpsertOffTransactionDetailParamsPtrs []*sqlc.UpsertOffTransactionDetailParams
}

// RefreshOffTransactionDetailsByOutTransNoAndOutOrderNo 优惠交易批量刷新优惠交易明细
func (s *OffTransactionEntityServiceImp) RefreshOffTransactionDetailsByOutTransNoAndOutOrderNo(ctx context.Context, q *sqlc.Queries, ptr *RefreshOffTransactionDetailsByOutTransNoAndOutOrderNoParam) (err error) {
	// slog := util.NewContextLogger(ctx, s.slog)
	err = s.DisableOffTransactionDetailsByOutTransNoAndOutOrderNo(ctx, q, &DisableOffTransactionDetailsByOutTransNoAndOutOrderNoParam{
		OutTransNo: ptr.OutTransNo,
		OutOrderNo: ptr.OutOrderNo,
	})
	if err != nil {
		return
	}
	for _, ptr := range ptr.UpsertOffTransactionDetailParamsPtrs {
		err = q.UpsertOffTransactionDetail(ctx, *ptr)
		if err != nil {
			return
		}
	}
	return
}

func (s *OffTransactionEntityServiceImp) BatchUpdateOffTransactionDetailOffAndCnt(ctx context.Context, q *sqlc.Queries, ptrs []*sqlc.UpdateOffTransactionDetailOffAndCntByTransactionIDAndDetailIDParams) (err error) {

	for _, ptr := range ptrs {
		err = q.UpdateOffTransactionDetailOffAndCntByTransactionIDAndDetailID(ctx, *ptr)
	}

	return
}
